package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.sc.nft.entity.vo.DingLianDealPracticalVO;
import com.sc.nft.service.ChainPeggingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 刘国航
 * @desc
 * @since 2024/12/11 17:49
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ChainPeggingServiceImpl implements ChainPeggingService {

    @Override
    public List<DingLianDealPracticalVO> dealPractical(List<DingLianDealPracticalVO> dealPracticalList, List<DingLianDealPracticalVO> dealTradingPracticalList) {
        //region 按藏品id分组
        Map<Long, DingLianDealPracticalVO> dealPracticalMap = CollectionUtil.isNotEmpty(dealPracticalList) ? dealPracticalList.stream().collect(Collectors.toMap(DingLianDealPracticalVO::getId, Function.identity())) : Maps.newHashMap();
        Map<Long, DingLianDealPracticalVO> dealTradingPracticalMap = CollectionUtil.isNotEmpty(dealTradingPracticalList) ? dealTradingPracticalList.stream().collect(Collectors.toMap(DingLianDealPracticalVO::getId, Function.identity())) : Maps.newHashMap();
        //endregion
        //region 获取去重后的藏品id
        Set<Long> dealPracticalIdSet = Sets.newHashSet();
        dealPracticalIdSet.addAll(dealPracticalMap.keySet());
        dealPracticalIdSet.addAll(dealTradingPracticalMap.keySet());
        //endregion
        if (CollectionUtil.isEmpty(dealPracticalIdSet)) {
            return Collections.emptyList();
        }
        List<DingLianDealPracticalVO> dealPracticalVOList = new ArrayList<>();
        for (Long id : dealPracticalIdSet) {
            //二级订单
            DingLianDealPracticalVO dealPractical = dealPracticalMap.get(id);
            //交易池订单
            DingLianDealPracticalVO dealTradingPractical = dealTradingPracticalMap.get(id);
            //是否有二级订单
            DingLianDealPracticalVO dingLianDealPracticalVO = Objects.nonNull(dealPractical) ? dealPractical : null;
            if (Objects.nonNull(dealTradingPractical)) {
                if (Objects.nonNull(dingLianDealPracticalVO)) {
                    //region 同时有二级订单和交易池订单
                    dingLianDealPracticalVO.setPrice(Objects.nonNull(dingLianDealPracticalVO.getPrice()) ? dingLianDealPracticalVO.getPrice() : dealTradingPractical.getPrice());
                    dingLianDealPracticalVO.setTxCount5m(dingLianDealPracticalVO.getTxCount5m() + dealTradingPractical.getTxCount5m());
                    dingLianDealPracticalVO.setTxCount1h(dingLianDealPracticalVO.getTxCount1h() + dealTradingPractical.getTxCount1h());
                    dingLianDealPracticalVO.setTxCount24h(dingLianDealPracticalVO.getTxCount24h() + dealTradingPractical.getTxCount24h());
                    dingLianDealPracticalVO.setTxCountToday(dingLianDealPracticalVO.getTxCountToday() + dealTradingPractical.getTxCountToday());
                    dingLianDealPracticalVO.setTxAmount5m(dingLianDealPracticalVO.getTxAmount5m() + dealTradingPractical.getTxAmount5m());
                    dingLianDealPracticalVO.setTxAmount1h(dingLianDealPracticalVO.getTxAmount1h() + dealTradingPractical.getTxAmount1h());
                    dingLianDealPracticalVO.setTxAmount24h(dingLianDealPracticalVO.getTxAmount24h() + dealTradingPractical.getTxAmount24h());
                    dingLianDealPracticalVO.setTxAmountToday(dingLianDealPracticalVO.getTxAmountToday() + dealTradingPractical.getTxAmountToday());
                    //endregion
                } else {
                    //region 只有交易池订单
                    dingLianDealPracticalVO = dealTradingPractical;
                    //endregion
                }
            }
            dealPracticalVOList.add(dingLianDealPracticalVO);
        }
        return dealPracticalVOList;
    }
}